
from sqlalchemy import insert, String, create_engine, select
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, Session


class Base(DeclarativeBase):
    pass



class Users(Base):
    __tablename__ = 'users'

    id : Mapped[int] = mapped_column(primary_key=True)
    name : Mapped[str] = mapped_column(String(20))

    def __repr__(self) -> str:
        return f"User(id= {self.id}, name={self.name})"

engine = create_engine("mysql+mysqlconnector://root:lilishop@192.168.3.47:3306/python?charset=utf8", echo=True)
Base.metadata.create_all(engine)

def main():
    # engine = create_engine("mysql+mysqlconnector://root:lilishop@192.168.3.47:3306/python?charset=utf8", echo=True)
    # Base.metadata.create_all(engine)
    dbRemove()




def dbInser():
    with Session(engine) as session:
        tom = Users(
            id = 1,
            name= "tom"
        )

        jhone = Users(id=2, name="jhone")
        session.add_all([tom, jhone])
        session.commit()

def dbSelect():
    with Session(engine) as session:
        query = select(Users).where(Users.name =="jhone")
        jhone =  session.scalar(query)
        print("user:", jhone)

def dbUpdate():
    with Session(engine) as session:
        query  = select(Users).where(Users.name == "jhone")
        jhone = session.scalars(query).one()

        jhone.name = "jhones"
        session.commit()

        # print("jhonenew :", jhonenew)

def dbRemove():
    with Session(engine) as session:
        query = session.get(Users, 2)

        session.delete(query)
        session.commit()




# stmt = insert(User).values(id=6, name="tom")

if __name__ == '__main__':
    main()